package com.mxprojects.gui
{
	import com.mxprojects.io.BoardCookie;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.StylesImageParser;
	import com.mxprojects.io.Utils;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.ColorTransform;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.StyleSheet;

	public class Styles
	{
		private static const MAIN_CSS:String = "main.css";
		private static const EXTRA_CSS:String = "extra.css";
		
		public static var css:StyleSheet = new StyleSheet();
		public static var extraCss:StyleSheet = new StyleSheet();
		
		private static var cssDataLocation:String;
		
		public function Styles(){}
		
		public static function setup(cssLocation:String, isExtraLoading:Boolean = false):void {
			//this will load the external css file into the forum
			cssDataLocation = cssLocation;
			
			var cssURL:String = MAIN_CSS;
			var cssLoader:URLLoader = new URLLoader();
			
			if(isExtraLoading){
				cssURL = EXTRA_CSS;
				cssLoader.addEventListener(Event.COMPLETE, parseExtraCSS);
			}else{
				cssLoader.addEventListener(Event.COMPLETE, parseMainCSS); //would not load unless weak was false *shrug*
			}
			cssLoader.load(new URLRequest(cssLocation+cssURL+"?"+Math.random()*328233));
			
			cssLoader.addEventListener(IOErrorEvent.IO_ERROR, loadForum, false, 0, true);
		}
		
		public static function setProperties(objectToSet:DisplayObjectContainer, cssStyle:String, isExtraCSS:Boolean = false):void {
			//whatever you pass to the object to set, will be done via the CSS
			var cssObject:Object = css.getStyle(cssStyle);
			if(isExtraCSS){
				cssObject = extraCss.getStyle(cssStyle);
			}
			
			for (var key:String in cssObject){		    
			    switch(key){
			    	case "color":
			    		//if we are dealing with color then we need to colortransform						
						objectToSet.transform.colorTransform = colorTransformCSS(colorToUint(cssStyle), cssObject.alpha);
			    	break;
			    	
			    	case "backgroundImage":
			    		//if we need to load something as an image just giv'r
				    	var parseBackgroundImage:StylesImageParser = new StylesImageParser();
				    	parseBackgroundImage.load(objectToSet, cssStyle);
			    	break;
			    	
					case "borderColor":
			    		//check the object for a border and possibly dividers and colour them
			    		/*
			    		for(var i:int = 0; i < objectToSet.parent.numChildren; i++){
			    			var child:DisplayObject = objectToSet.parent.getChildAt(i);
			    			if(child.name == "border" || child.name == "divider"){
			    				//colourize the border/divider
			    				child.transform.colorTransform = colorTransformCSS(colorToUint(cssStyle, "borderColor"));
			    			}
			    		}
			    		*/
			    	break;
			    	
			    	default:
			    		//just apply whatever the key is to the object
			    		if(objectToSet.hasOwnProperty(key)){
			    			objectToSet[key] = cssObject[key];
			    		}
			    	break;
			    }
			} 
		}
		
		public static function colorToUint(cssStyle:String, parameter:String = "color"):uint {
			return uint("0x" + String(css.getStyle(cssStyle)[parameter]).substring(1));
		}
		
		public static function colorTransformCSS(color:uint, alpha:String):ColorTransform {
			var colorArray:Array = Utils.hexToRGB(color);
			var alphaValue:Number = 1;
			if(alpha != null && alpha != "undefined"){
				alphaValue = Number(alpha);
			}
			
			return new ColorTransform(0, 0, 0, alphaValue, colorArray[0], colorArray[1], colorArray[2], 0);
		}
		
		public static function clearBackgroundImage(objectToClear:DisplayObjectContainer):void {
			//this will just clear off a background if there is one
			var background:DisplayObject = objectToClear.parent.getChildByName("backgroundImage");
			if(background != null){
				objectToClear.parent.removeChild(background);
			}
		}
		
		public static function getCleanValue(cssStyle:String, parameter:String):String {
			//this will take the value of the parameter, if it's a string it'll strip the quotes
			var value:String = css.getStyle(cssStyle)[parameter];
			if(value == null){
				return "";	
			}
			
			if(value.substr(0,1) == "\""){
				//strip it
				value = value.substr(1, value.length - 2);
			}else if(value.substr(-2) == "px"){
				value = value.substr(0, value.length - 2);
			}
			
			return value;
		}
		
		private static function parseMainCSS(event:Event):void {
			//parse the css into our css object
			css.parseCSS(event.target.data);
			
			//load the extra CSS
			setup(cssDataLocation, true);
		}
		
		private static function parseExtraCSS(event:Event):void {
			//parse the css into our css object
			extraCss.parseCSS(event.target.data);
			
			//load the rest of the forum
			loadForum();
		}
		
		private static function loadForum(event:IOErrorEvent = null):void {
			//build a right-click menu
			Utils.createRightClickMenu();
			
			//prase the first bit of CSS starting in the header
			MXP3.header.setupCSS();
			
			//let's also load the user info if they are already saved to this machine
			BoardCookie.loadCookie();
		}
	}
}